package link.ten_fei.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import link.ten_fei.Bean.Doc_User;
import link.ten_fei.util.ConnectionFactory;

public class AddUserDaoImpl implements AddUserDao {

	@Override
	public Doc_User findByNameOrId(String doc_name, String doc_age, int id) {
		// 1.获取数据库连接
		Connection connection = ConnectionFactory.getConnection();
		PreparedStatement preparedStatement = null;
		ResultSet rs = null;
		try {
			if (id != 0) {
				// 2.准备sql语句
				String sql = "select * from doc_user where id=?";
				// 3.获取连接通路
				preparedStatement = connection.prepareStatement(sql);
				// 4.索引
				preparedStatement.setInt(1, id);
			} else {
				// 2.准备sql语句
				String sql = "select * from doc_user where doc_name=? and doc_age=?";
				// 3.获取连接通路
				preparedStatement = connection.prepareStatement(sql);
				// 4.索引
				preparedStatement.setString(1, doc_name);
				preparedStatement.setString(2, doc_age);
			}
			// 5.执行sql语句,返回查询的结果,封装在rs中
			rs = preparedStatement.executeQuery();

			// 查询的结果包含表头信息
			while (rs.next()) {
				Doc_User user = new Doc_User();
				// 每一行的数据封装在一个实体bean中，根据字段获取字段值，该字段是什么类型就get什么类型
				user.setId(rs.getInt("id"));
				user.setDoc_name(rs.getString("doc_name"));
				user.setDoc_age(rs.getString("doc_age"));
				return user;
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			ConnectionFactory.close(connection, preparedStatement, rs);
		}

		return null;
	}

	@Override
	public int save(Doc_User user) {
		// 1.获取数据库连接
		Connection connection = ConnectionFactory.getConnection();
		PreparedStatement preparedStatement = null;
		int num = 0;
		try {
			// 2.准备sql语句
			String sql = "INSERT INTO doc_user(doc_name,doc_username,doc_password,doc_sex,doc_age,doc_title,doc_department,doc_tel,doc_judge) VALUES(?,?,?,?,?,?,?,?,?)";
			// 3.获取连接通路
			preparedStatement = connection.prepareStatement(sql);
			// 4索引
			preparedStatement.setString(1, user.getDoc_name());
			preparedStatement.setString(2, user.getDoc_username());
			preparedStatement.setString(3, user.getDoc_password());
			preparedStatement.setString(4, user.getDoc_sex());
			preparedStatement.setString(5, user.getDoc_age());
			preparedStatement.setString(6, user.getDoc_title());
			preparedStatement.setString(7, user.getDoc_department());
			preparedStatement.setString(8, user.getDoc_tel());
			preparedStatement.setInt(9, user.getDoc_judge());
			// 5.执行sql语句,进行结果更新到数据库中,返回值为更新影响行数
			num = preparedStatement.executeUpdate();

		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			ConnectionFactory.close(connection, preparedStatement, null);
		}
		return num;

	}

	@Override
	public int change(Doc_User user, int id) {
		Connection connection = ConnectionFactory.getConnection();
		PreparedStatement preparedStatement = null;
		int num = 0;
		try {
			String sql = "update doc_user set doc_name=?,doc_sex=?,doc_age=?,doc_title=?,doc_department=?,doc_tel=? where id=?";
			preparedStatement = connection.prepareStatement(sql);
			preparedStatement.setString(1, user.getDoc_name());
			preparedStatement.setString(2, user.getDoc_sex());
			preparedStatement.setString(3, user.getDoc_age());
			preparedStatement.setString(4, user.getDoc_title());
			preparedStatement.setString(5, user.getDoc_department());
			preparedStatement.setString(6, user.getDoc_tel());
			preparedStatement.setInt(7, id);
			num = preparedStatement.executeUpdate();
			return num;
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			ConnectionFactory.close(connection, preparedStatement, null);
		}
		return 0;
	}
}
